#ifndef REQUESTDATA
#define REQUESTDATA

#include <string>
#include <unordered_map>
#include "timer.h"
class mytimer; //前向声明

const int STATE_PARSE_URI = 1;
const int STATE_PARSE_HEADERS = 2;
const int STATE_RECV_BODY = 3;
const int STATE_ANALYSIS = 4;
const int STATE_FINISH = 5;

const int MAX_BUFF = 4096;

const int AGAIN_MAX_TIMES = 200;

const int PARSE_URI_AGAIN = -1;
const int PARSE_URI_ERROR = -2;
const int PARSE_URI_SUCCESS = 0;

const int PARSE_HEADER_AGAIN = -1;
const int PARSE_HEADER_ERROR = -2;
const int PARSE_HEADER_SUCCESS = 0;

const int ANALYSIS_ERROR = -2;
const int ANALYSIS_SUCCESS = 0;

const int METHOD_POST = 1;
const int METHOD_GET = 2;
const int HTTP_10 = 1;
const int HTTP_11 = 2;
const int EPOLL_WAIT_TIME = 500;

class MimeType { //  描述消息内容
private:
    static pthread_mutex_t lock; //对于MimeType的互斥保护
    static std::unordered_map <std::string, std::string> mime;
    MimeType();
    MimeType(const MimeType &m);
public:
    static std::string getMime(const std::string &suffix);
};

enum HeadersState {
    h_start = 0,
    h_key,
    h_colon,
    h_spaces_after_colon,
    h_value,
    h_CR,
    h_LF,
    h_end_CR,
    h_end_LF
};


struct requestData {
private:
    int fd;
    int epollfd;
    mytimer* timer;

    std::string content;// content的内容用完就清
    int againTimes;

    int method;
    int HTTPversion;

    std::string file_name; //请求的文件名
    int now_read_pos;
    int state;
    int h_state;
    bool isfinish;
    bool keep_alive; //是否是长连接
    std::unordered_map <std::string, std::string> headers;  //存储请求头键值对

private:
    int parse_URI();
    int parse_Headers();
    int analysisRequest(); //响应请求
public:
    requestData();
    requestData(int _epollfd, int _fd);
    ~requestData();
    void handleRequest();
    int getFd();
    void setFd(int _fd);
    void setTimer(mytimer* timer);
    void reset();
    void handleError(int fd, int err_num, std::string short_msg);
};


#endif